var device=request("from")
var phone=request("phone")
var flag=request("flag")
$(function(){
	//微信中检查phone是否绑定开始---------------------------------------------------
	if(device!="app"){
		if(sessionStorage.getItem("phone")!=""&&sessionStorage.getItem("phone")!=undefined&&sessionStorage.getItem("phone")!=null){
			phone=sessionStorage.getItem("phone");
		}
		else{
			var code=request("code");
			$.ajax({
			    url: DATA_URL+"checkwxbind",
			    data: "code="+code,
			    type: "post",
			    async: false,
			    dataType: 'json',
			    timeout: 5000,
			    success:function (data) {
			    	if(data.code=="0"){
			    		if(data.info.is_bind==1){
			    			phone=data.info.phone;
			    			sessionStorage.setItem("phone",data.info.phone);
			    		}
			    		else{
			    			var open_id=data.info.open_id;
			    			location.href="binding_phone.html?open_id="+open_id
			    		}
			    	}
					else{
						$("#prompt").text(data.msg);
			            $(".prompt").fadeIn(300,function(){
			                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
			            })
					}
			    },
			    error:function () {
			    	$("#prompt").text("您的网络不给力哦~");
			        $(".prompt").fadeIn(300,function(){
			            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
			        })
			    }
			});
		}
		
	}
	//微信中检查phone是否绑定结束-----------------------------------------------------------
	
	$("#base").click(function(){
		if(flag=="android"){
			location.href="qicheng://base_info";
		}
		else{
			location.href="info_eidt.html?phone="+phone+"&from="+device
		}
	})
	$("#addr").click(function(){
		if(flag=="android"){
			location.href="qicheng://car_place";
		}
		else{
			location.href="place_pic.html?phone="+phone+"&from="+device
		}
	})
	$("#introduce").click(function(){
		location.href="characteristics.html?phone="+phone+"&from="+device
	})
	$("#model .txt").click(function(){
		location.href="class_type.html?phone="+phone+"&from="+device
	})
	//编辑班型
	$(".model_list").on("click",".edit",function(){
		var class_id=$(this).parents("tr").attr("id");
		var class_name=$(this).parents("tr").find(".name").text();
		var car_type=$(this).parents("tr").find(".type").text();
		var price=$(this).parents("tr").find(".price span").text();
		var desc=$(this).parents("tr").find(".detail").text();
		location.href="class_type.html?phone="+phone+"&from="+device+"&class_id="+class_id+"&class_name="+class_name+"&car_type="+car_type+"&price="+price+"&desc="+desc;
	})
	//删除班型
	$(".model_list").on("click",".delete",function(){
		var obj=$(this).parents("tr");
		$.ajax({
		    url: DATA_URL+"delclassinfo",
		    data: "class_id="+$(this).parents("tr").attr("id"),
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		obj.remove();
		    		if($(".model_list tr").size()==0){
		    			$("#model .info").css("border-bottom","0")
		    		}
		    		$("#prompt").text("删除成功");
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
		
	})
	//获取信息状态
	get_state()
	function get_state(){
		$.ajax({
		    url: DATA_URL+"couchinfostatus",
		    data: "phone="+phone,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		$("#base").text(data.info.base)
		    		$("#addr").text(data.info.addr)
		    		$("#introduce").text(data.info.introduce)
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
	//获取班型信息
	get_class()
	function get_class(){
		$.ajax({
		    url: DATA_URL+"getclassinfo",
		    data: "phone="+phone,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		var class_info="";
		    		$.each(data.info,function(index,val){
		    			class_info+="<tr id='"+val.id+"'>\
										<td width='50%'>\
											<div class='title'>\
												<span class='name'>"+val.class_name+"</span>\
												<span class='type'>"+val.car_type+"</span>\
											</div>\
											<div class='detail'>"+val.desc+"</div>\
										</td>\
										<td width='25%'>\
											<div class='price'>￥<span>"+val.price+"</span></div>\
										</td>\
										<td width='25%'>\
											<div class='edit'></div>\
											<div class='delete'></div>\
										</td>\
									</tr>"
		    		})
		    		$(".model_list").html(class_info);
		    		if($(".model_list tr").size()==0){
		    			$("#model .info").css("border-bottom","0")
		    		}
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	}
	
	
	//生成名片
	$(".submit").click(function(){
		$.ajax({
		    url: DATA_URL+"createcart",
		    data: "phone="+phone,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
		    		$("#prompt").text("成功生成名片");
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300,
		                	function(){
		                		location.href="instructor_detail.html?couch_phone="+phone+"&from="+device;
		                	}
		                )},3000)
		            })
		    	}
		    	else if(data.code=="2"){
		    		$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300,
		                	function(){
		                		location.href="instructor_detail.html?couch_phone="+phone+"&from="+device;
		                	}
		                )},3000)
		            })
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
	})
	
	$("#load").click(function(){
		get_state()
		get_class()
	})
})
